package com.sbpm.crmbpm.server.jbpm.users;

import com.sbpm.crmbpm.server.dao.UserDao;
import com.sbpm.crmbpm.server.exceptions.ObjectNotFoundException;
import com.sbpm.crmbpm.shared.data.organisation.Role;
import com.sbpm.crmbpm.shared.data.organisation.User;
import org.kie.internal.task.api.UserGroupCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserGroupCallbackImpl implements UserGroupCallback
{

    @Autowired
    private UserDao userDao;

    @Override
    public boolean existsUser( String id )
    {
        try
        {
            User user = userDao.findActiveUserById( id );
            return true;
        }
        catch ( ObjectNotFoundException e )
        {
            return false;
        }

    }

    @Override
    public boolean existsGroup( String id )
    {
        try
        {
            Role user = userDao.findRoleById( id );
            return true;
        }
        catch ( ObjectNotFoundException e )
        {
            return false;
        }
    }

    @Override
    public List< String > getGroupsForUser( String userId, List< String > list, List< String > list1 )
    {
        List<Role> roles = userDao.getRolesForUser( userId );
        List<String> groups = new ArrayList<>();
        for ( Role role : roles )
        {
            groups.add( role.getRoleId() );
        }
        return groups;
    }


}
